package com.zxy.study.ui.behavior;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;

import com.zxy.study.R;

import java.lang.ref.WeakReference;


/*
 *  @项目名：  study
 *  @包名：    com.zxy.study.ui.behavior
 *  @文件名:   UCBehavior
 *  @创建者:   zhangxy
 *  @创建时间:  2018/11/2 10:40
 *  @描述：    TODO
 */
public class UCBehavior extends CoordinatorLayout.Behavior<View> {

	private WeakReference<View> mDependencyView;
	private WeakReference<View> mTabBarView;
	private WeakReference<View> mMaskView;

	/*滚动控件可移动的最大距离*/
	private float mMaxTranslationY;
	/*顶部banner最大高度*/
	private int mHeadHeight;
	/*头部标签栏的高度*/
	private int mTabBarHeight;

	/*是否收缩到顶部*/
	private boolean isCollapse;

	private ValueAnimator mValueAnimator;


	public UCBehavior() {
	}

	public UCBehavior(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
		return dependency.getId() == R.id.fl;
	}

	@Override
	public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
		init(parent);
		CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
		if (lp.height == CoordinatorLayout.LayoutParams.MATCH_PARENT) {
			child.layout(0, 0, parent.getWidth(), parent.getHeight() - mTabBarHeight);
			return true;
		}
		return super.onLayoutChild(parent, child, layoutDirection);
	}

	private void init(CoordinatorLayout parent) {
		if (mDependencyView == null || mDependencyView.get() == null) {
			mDependencyView = new WeakReference<>(parent.findViewById(R.id.fl));
		}
		if (mHeadHeight <= 0) {
			mDependencyView.get().measure(0, 0);
			mHeadHeight = mDependencyView.get().getMeasuredHeight();
		}
		if (mTabBarView == null || mTabBarView.get() == null) {
			mTabBarView = new WeakReference<>(parent.findViewById(R.id.view_title_bar));
		}
		if (mTabBarHeight <= 0) {
			mTabBarView.get().measure(0, 0);
			mTabBarHeight = mTabBarView.get().getMeasuredHeight();
		}
		if (mMaxTranslationY <= 0 && mTabBarHeight > 0 && mHeadHeight > 0) {
			mMaxTranslationY = mHeadHeight - mTabBarHeight;
		}
		if (mMaskView == null || mMaskView.get() != null) {
			mMaskView = new WeakReference<>(parent.findViewById(R.id.iv_mask));
		}
	}

	@Override
	public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
		float newTranslationY = mHeadHeight + dependency.getTranslationY() * 2;
		newTranslationY = newTranslationY < mTabBarHeight ? mTabBarHeight : newTranslationY;
		child.setTranslationY(newTranslationY);

		if (mTabBarView.get() != null) {
			float translationY = child.getTranslationY();
			float dy = translationY <= 2 * mTabBarHeight ? mTabBarHeight - translationY : -mTabBarHeight;
			dy = dy > 0 ? 0 : dy;
			mTabBarView.get().setTranslationY(dy);
		}
		return super.onDependentViewChanged(parent, child, dependency);
	}

	@Override
	public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
		return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
	}

	@Override
	public void onNestedScrollAccepted(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
		if (mValueAnimator != null && mValueAnimator.isStarted()) {
			mValueAnimator.cancel();
		}
	}

	@Override
	public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
		if (isCollapse || dy <= 0) {
			return;
		}
		//处理向上滑动
		if (mDependencyView.get() != null) {
			View dependency = mDependencyView.get();
			if (Math.abs(dependency.getTranslationY()) != mMaxTranslationY / 2) {
				float newY = dependency.getTranslationY() - dy / 2;
				newY = newY < -mMaxTranslationY / 2 ? -mMaxTranslationY / 2 : newY;
				dependency.setTranslationY(newY);

				consumed[1] = dy;

				if (mMaskView.get().getAlpha() != 1) {
					mMaskView.get().setAlpha(1.0f);
				}
			}
		}
	}

	@Override
	public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
		if (isCollapse || dyUnconsumed > 0) {
			return;
		}
		//处理向下滑动
		if (mDependencyView.get() != null) {
			View dependency = mDependencyView.get();
			if (Math.abs(dependency.getTranslationY()) != 0) {
				float newY = dependency.getTranslationY() - dyUnconsumed / 2;
				newY = newY > 0 ? 0 : newY;
				dependency.setTranslationY(newY);

				if (mMaskView.get().getAlpha() != 1) {
					mMaskView.get().setAlpha(1.0f);
				}
			}
		}
	}

	@Override
	public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int type) {
		super.onStopNestedScroll(coordinatorLayout, child, target, type);

		float translationY = mDependencyView.get().getTranslationY();
		if (Math.abs(2 * translationY) > mMaxTranslationY / 2) {
			//收缩
			onUserStopDrag(translationY, -mMaxTranslationY / 2);
		} else {
			//张开
			onUserStopDrag(translationY, 0);

		}
	}

	private void onUserStopDrag(float start,float end) {
		if (mValueAnimator == null) {
			mValueAnimator = ValueAnimator.ofFloat(0, 0);
			mValueAnimator.setDuration(200);
			mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
				@Override
				public void onAnimationUpdate(ValueAnimator animation) {
					float value = (float) animation.getAnimatedValue();
					mDependencyView.get().setTranslationY(value);
				}
			});

			mValueAnimator.addListener(new AnimatorListenerAdapter() {
				@Override
				public void onAnimationEnd(Animator animation) {
					super.onAnimationEnd(animation);
					mMaskView.get().setAlpha(0);

					float translationY = mDependencyView.get().getTranslationY();
					if(translationY + mMaxTranslationY /2 == 0){
						isCollapse = true;
					}else if(translationY == 0){
						isCollapse = false;
					}
				}
			});
		}
		mValueAnimator.setFloatValues(start, end);
		mValueAnimator.start();
	}


	public boolean expandView(){
		if(isCollapse){
			onUserStopDrag(mDependencyView.get().getTranslationY(),0);
			return true;
		}
		return false;
	}
}
